home *** CD-ROM | disk | FTP | other *** search
/ Developer CD Series 1992 June: ROMin Holiday / ADC Developer CD (1992-06) (''ROMin Holiday'')_iso / Developer Connection - 06-1992.iso / Periodicals / develop / develop 10 code / Is it Art? / ArtMaker / PaintDlogs.c < prev    next >
Encoding:
C/C++ Source or Header  |  1992-04-08  |  9.1 KB  |  373 lines  |  [TEXT/KAHL]

  1. #include    "Painterly.h"
  2. #include    "DialogUtils.h"
  3.  
  4. extern DocumentRecord    gDstDoc;            /* The doc record for the destination */
  5. extern CWindowPtr        gDstWindPtr;        /* the destination window */
  6. extern GWorldPtr        gUndoBuffer;
  7. extern short            gOrderedIncrement;
  8. extern Boolean            gRandomStrokes;
  9.  
  10. /* Protos for a couple internal (to this file) routines */
  11. pascal void DimIt(WindowPtr dwind, short dinum);
  12. pascal Boolean TabFilter(DialogPtr dptr, EventRecord *event, short *item);
  13.  
  14. /* This is the Pen Size dialog */
  15. void PenDlog(void)
  16. {
  17.     DialogPtr            dptr;
  18.     short                hit, kind;
  19.     Handle                itmhndl;
  20.     Rect                rect;
  21.     PenState            thePen;
  22.     CGrafPtr            oldport;
  23.     GDHandle            olddev;
  24.     
  25.     #define PENX 4 /* The Pen Width text item */
  26.     #define PENY 5 /* The Pen Height text item */
  27.     
  28.     /* Save the current port and device */
  29.     GetGWorld(&oldport, &olddev);
  30.     
  31.     /* Get the dialog (invisible) */
  32.     dptr = GetNewDialog(kPenDlogID, nil, (Ptr)(-1));
  33.     if(dptr == nil)
  34.     {
  35.         DoErrorAlert(kNoResStr, 0);
  36.         return;
  37.     }
  38.     
  39.     /* OK button Outline */
  40.     GetDItem(dptr, kOKOutline, &kind, &itmhndl, &rect);
  41.     SetDItem(dptr, kOKOutline, userItem, (Handle)BtnItem, &rect);
  42.     
  43.     /* Other user Items... */
  44.     
  45.     /* Set Values of text items... */
  46.     SetGWorld(gDstDoc.world, nil);
  47.     GetPenState(&thePen);
  48.     ShortToDlog(thePen.pnSize.h, dptr, PENX);
  49.     ShortToDlog(thePen.pnSize.v, dptr, PENY);
  50.     SetGWorld(oldport, olddev);
  51.         
  52.     /* Do it... */
  53.     SelIText(dptr, PENX, 0, 32767);
  54.     ShowWindow(dptr);
  55.     while(true)                                    /* Loop until break...*/
  56.     {
  57.         Boolean     out = false;
  58.         
  59.         ModalDialog(NumFilter, &hit); /* Num Filter only allows numeric input */
  60.         switch(hit)
  61.         {
  62.             case kOKButton:                        /* OK button hit...*/
  63.                 if(InRange(dptr, PENX, 1, 100))
  64.                 {
  65.                     if(InRange(dptr, PENY, 1, 100))
  66.                     {
  67.                         short    penx, peny;
  68.                         
  69.                         /* Need to set the pen size in both destination ports AND
  70.                         the Undo buffer, if it exists, since the user may switch
  71.                         to it at any time by doing an undo */
  72.                         penx = DlogToShort(dptr, PENX);
  73.                         peny = DlogToShort(dptr, PENY);
  74.                         
  75.                         SetGWorld(gDstWindPtr, olddev);
  76.                         PenSize(penx, peny);
  77.                         SetGWorld(gDstDoc.world, nil);
  78.                         PenSize(penx, peny);
  79.                         if(gUndoBuffer != nil)
  80.                         {
  81.                             SetGWorld(gUndoBuffer, nil);
  82.                             PenSize(penx, peny);
  83.                         }
  84.                         SetGWorld(oldport, olddev);
  85.                         out = true;
  86.                     }
  87.                 }
  88.                 break;
  89.     
  90.             case kCancelButton:                        /* Cancel button hit...*/
  91.                 out = true;
  92.                 break;
  93.     
  94.             default:                    /* Anything else hit... */
  95.                 continue;
  96.         }
  97.         if(out)
  98.             break;
  99.     }
  100.     InitCursor();
  101.     DisposDialog(dptr);
  102. }
  103.  
  104. void AutoPaintSetupDlog(void)
  105. {
  106.     DialogPtr    dptr;
  107.     short        hit, kind;
  108.     Handle        itmhndl;
  109.     Rect        rect;
  110.     
  111.     #define kIncAmnt    6 /* The text item for the no. of pixels to skip */
  112.     #define kRandRad    7 /* The "Random Strokes" radio button */
  113.     #define kOrdRad        8 /* The "Ordered Strokes" radio button */
  114.     #define kGrayBox    9 /* A user item that covers the stuff we may need to gray out */
  115.     #define kDummyItem    10 /* A text item that is outside the visible dialog window: when
  116.                             the visible text item is grayed out, this one is selected to
  117.                             hide the insertion point. Otherwise you get a flashing
  118.                             insertion point in the grayed item, which looks wierd */
  119.     
  120.     dptr = GetNewDialog(kAutoPaintID, nil, (Ptr)(-1));
  121.     if(dptr == nil)
  122.     {
  123.         DoErrorAlert(kNoResStr, 0);
  124.         return;
  125.     }
  126.     
  127.     /* OK button Outline */
  128.     GetDItem(dptr, kOKOutline, &kind, &itmhndl, &rect);
  129.     SetDItem(dptr, kOKOutline, userItem, (Handle)BtnItem, &rect);
  130.     
  131.     /* The dimming Box */
  132.     GetDItem(dptr, kGrayBox, &kind, &itmhndl, &rect);
  133.     SetDItem(dptr, kGrayBox, userItem, (Handle)DimIt, &rect);
  134.  
  135.     /* Set Values...the true gOrderedIncrement is one more than the number of pixels to 
  136.     skip, which is what the user enters. This is because gOrderedIncrement is added to 
  137.     the previous point */
  138.     ShortToDlog(gOrderedIncrement - 1, dptr, kIncAmnt);
  139.     
  140.     GetDItem(dptr, kRandRad, &kind, &itmhndl, &rect);
  141.     SetCtlValue(itmhndl, gRandomStrokes ? 1 : 0);
  142.         
  143.     GetDItem(dptr, kOrdRad, &kind, &itmhndl, &rect);
  144.     SetCtlValue(itmhndl, gRandomStrokes ? 0 : 1);
  145.  
  146.     if(gRandomStrokes)
  147.         SelIText(dptr, kDummyItem, 0, 0);
  148.     else
  149.         SelIText(dptr, kIncAmnt, 0, 32767);
  150.  
  151.     /* Do it... */
  152.     ShowWindow(dptr);
  153.     while(true)                                    /* Loop until break...*/
  154.     {
  155.         Boolean     out = false;
  156.         
  157.         ModalDialog(TabFilter, &hit);
  158.         switch(hit)
  159.         {
  160.             case kOKButton:                        /* OK button hit...*/
  161.                 GetDItem(dptr, kRandRad, &kind, &itmhndl, &rect);
  162.                 gRandomStrokes = (GetCtlValue(itmhndl) ? true : false);
  163.                 if(gRandomStrokes == false)
  164.                 {
  165.                     if(InRange(dptr, kIncAmnt, 0, 1000))
  166.                     {
  167.                         gOrderedIncrement = DlogToShort(dptr, kIncAmnt) + 1;
  168.                         out = true;
  169.                     }
  170.                 }
  171.                 else
  172.                     out = true;
  173.                 break;
  174.     
  175.             case kCancelButton:                        /* Cancel button hit...*/
  176.                 out = true;
  177.                 break;
  178.     
  179.             case kRandRad:                    /* Radio button hit...*/
  180.             case kOrdRad:
  181.                 GetDItem(dptr, hit, &kind, &itmhndl, &rect);
  182.                 if(GetCtlValue(itmhndl) == 0)
  183.                 {
  184.                     SetCtlValue(itmhndl, 1);
  185.                     GetDItem(dptr, (hit == kRandRad ? kOrdRad : kRandRad), &kind, &itmhndl, &rect);
  186.                     SetCtlValue(itmhndl, (GetCtlValue(itmhndl) ? 0 : 1));
  187.                     GetDItem(dptr, kGrayBox, &kind, &itmhndl, &rect);
  188.                     SetPort(dptr);
  189.                     InvalRect(&rect);
  190.                     if(hit == kRandRad) /* disabling text item */
  191.                     {
  192.                         SelIText(dptr, kDummyItem, 0, 0);
  193.                     }
  194.                     else
  195.                         SelIText(dptr, kIncAmnt, 0, 32767);
  196.  
  197.                 }
  198.                 continue;
  199.  
  200.             default:                    /* Anything else hit... */
  201.                 continue;
  202.         }
  203.         if(out)
  204.             break;
  205.     }
  206.     InitCursor();
  207.     DisposDialog(dptr);
  208. }
  209.  
  210. /* This userItem dims itself (and anything under it) if the kRandRad radio button is
  211.     on */
  212. pascal void DimIt(WindowPtr dwind, short dinum)
  213. {
  214.     ControlHandle tempCont;
  215.     
  216.     tempCont = SnatchHandle(dwind, kRandRad);
  217.     if (GetCtlValue(tempCont) == 1)
  218.     {
  219.         PenState thePen;
  220.         short itemType;
  221.         Handle itemHandle;
  222.         Rect dimRect;
  223.         
  224.         GetPenState(&thePen);
  225.         GetDItem(dwind, dinum, &itemType, &itemHandle, &dimRect);
  226.         PenMode(notPatBic);
  227.         PenPat(qd.gray);
  228.         PaintRect(&dimRect);
  229.         SetPenState(&thePen);
  230.     }
  231. }
  232.  
  233. pascal Boolean TabFilter(DialogPtr dptr, EventRecord *event, short *item)
  234. {
  235.     char         theKey;
  236.     Boolean        returnVal = false, grayed;
  237.     short        hit, kind;
  238.     Handle        itmhndl;
  239.     Rect        rect;
  240.     
  241.     /* If the edit text isn't grayed out, Change the cursor to an I Beam if it's over
  242.         the active editText item */
  243.     grayed = GetCtlValue(SnatchHandle(dptr, kRandRad));
  244.     if(!grayed)
  245.         IBeamIt(dptr);
  246.     
  247.     /* Standard key filtering */
  248.     returnVal = StdKeyFilter(dptr, event, item);
  249.     
  250.     /* if that didn't handle it... */
  251.     if(returnVal == false)
  252.     {
  253.         /* If the edit text is grayed out,  capture mouseDowns in the edit item to
  254.             prevent the user activating it */
  255.         if(grayed)
  256.         {
  257.             if(event->what == mouseDown)
  258.             {
  259.                 Point        here;
  260.                 
  261.                 here = event->where;
  262.                 SetPort(dptr);
  263.                 GlobalToLocal(&here);
  264.                 GetDItem(dptr, kIncAmnt, &kind, &itmhndl, &rect);
  265.                 if(PtInRect(here, &rect))
  266.                     returnVal = true;
  267.             }
  268.         }
  269.     }
  270.             
  271.     /* if that didn't handle it, look at keydowns. Trap tabs if the text is grayed out 
  272.         so the user can't tab into it, otherwise allow only numeric input if the text 
  273.         is active*/
  274.     if(returnVal == false && ( event->what == keyDown || event->what == autoKey ))
  275.     {
  276.         theKey = event->message & charCodeMask;
  277.         if(grayed)
  278.         {
  279.             if(theKey == kTabKey)
  280.             {
  281.                 /* If the Random Radio Button is set, then don't let Tab key
  282.                     go to edit text box */
  283.                 if(grayed)
  284.                     returnVal = true;
  285.             }
  286.         }
  287.         else
  288.         {
  289.             /* We're only allowing numeric characters */
  290.             if(theKey > kLastCntrlKey && theKey < kDeleteKey)         /* Printable Ascii? */
  291.             {
  292.                 if (theKey < '0' || theKey > '9') /* not a number */
  293.                 {
  294.                     SysBeep(1);            /* complain a little */
  295.                     returnVal = true;
  296.                 }
  297.             }
  298.             else
  299.                 returnVal = false;
  300.         }
  301.     }        
  302.     return(returnVal);
  303. }
  304.  
  305. /* The alert that appears if the offworlds are less than 32 bits deep */
  306. void DepthAlert(short offdepth)
  307. {
  308.     Str255    depthStr, colorsStr;
  309.     long    colors;
  310.     
  311.     NumToString((long)offdepth, depthStr);
  312.     colors = 1L << offdepth;
  313.     NumToString(colors, colorsStr);
  314.     ParamText(colorsStr, depthStr, nil, nil);
  315.     NoteAlert(kDepthAlertID, nil);
  316. }
  317.  
  318. /* Puts up the "Save Changes?" dialog, and returns the number of the button hit */
  319. short SaveChangesDlog(void)
  320. {
  321.     Str255        fileName;
  322.     DialogPtr    dptr;
  323.     short        hit, kind;
  324.     Handle        itmhndl;
  325.     Rect        rect;
  326.     Boolean        rslt = false;
  327.  
  328.     #define NOSAVEBUTTON     4
  329.     
  330.     /* Get the title of the front window, and use ParamText to set it in
  331.         the dialog */
  332.     GetWTitle(FrontWindow(), fileName);
  333.     ParamText(fileName, "\p", "\p", "\p");
  334.     
  335.     dptr = GetNewDialog(kSaveChangesDlogID, nil, (Ptr)(-1));
  336.     if(dptr == nil)
  337.     {
  338.         DoErrorAlert(kNoResStr, 0);
  339.         return kCancelButton;
  340.     }
  341.     
  342.     /* OK button Outline */
  343.     GetDItem(dptr, kOKOutline, &kind, &itmhndl, &rect);
  344.     SetDItem(dptr, kOKOutline, userItem, (Handle)BtnItem, &rect);
  345.     
  346.     /* Do it... */
  347.     ShowWindow(dptr);
  348.     while(true)                                    /* Loop until break...*/
  349.     {
  350.         Boolean     out = false;
  351.         
  352.         ModalDialog(StdKeyFilter, &hit);
  353.         switch(hit)
  354.         {
  355.             case kOKButton:
  356.             case kCancelButton:
  357.             case NOSAVEBUTTON:
  358.                 rslt = hit;
  359.                 out = true;
  360.                 break;
  361.     
  362.             default:                    /* Anything else hit... */
  363.                 continue;
  364.         }
  365.         if(out)
  366.             break;
  367.     }
  368.     InitCursor();
  369.     DisposDialog(dptr);
  370.     return rslt;
  371. }
  372.  
  373.